package com.jingdianjichi.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthUserDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthUserDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthUserBO;
import com.jingdianjichi.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;


@Slf4j
@RestController
@RequestMapping("/user/")
public class UserController {
    @Resource
    private AuthUserDomainService authUserDomainService;


    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("captcha") String captcha) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(captcha), "验证码不能为空");
            SaTokenInfo saTokenInfo = authUserDomainService.deLogin(captcha);
            return Result.ok(saTokenInfo);
        } catch (Exception e) {
            log.info("UserController.doLogin.error:{}", e.getMessage());
            return Result.fail("用户登陆失败");
        }
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    /**
     * 注册用户
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.register.authUserDTO:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空");
            Preconditions.checkNotNull(authUserDTO.getPassword(), "密码不能为空");
            Preconditions.checkNotNull(authUserDTO.getEmail(), "邮件不能为空");

            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean result = authUserDomainService.register(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController.register.error:{}", e.getMessage());
            return Result.fail("注册角色失败");
        }
    }

    /**
     * 更新用户
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.authUserDTO:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getId(), "用户主键不能为空");

            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean result = authUserDomainService.update(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage());
            return Result.fail("更新角色失败");
        }
    }

    /**
     * 删除用户
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.delete.authUserDTO:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getId(), "用户主键不能为空");

            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean result = authUserDomainService.delete(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController.delete.error:{}", e.getMessage());
            return Result.fail("删除角色失败");
        }
    }

    /**
     * 用户启用/禁用
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.changeStatus.authUserDTO:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getId(), "用户主键不能为空");
            Preconditions.checkNotNull(authUserDTO.getStatus(), "用户状态不能为空");

            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean result = authUserDomainService.changeStatus(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController.changeStatus.error:{}", e.getMessage());
            return Result.fail("用户启用/禁用失败");
        }
    }

    @RequestMapping("getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.getUserInfo.authUserDTO:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空");

            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            AuthUserBO authUserBO1 = authUserDomainService.getUserInfo(authUserBO);
            if (Objects.isNull(authUserBO1)) {
                return Result.ok();
            }
            AuthUserDTO result = AuthUserDTOConverter.INSTANCE.convertBOToDTO(authUserBO1);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController.getUserInfo.error:{}", e.getMessage());
            return Result.fail("获取用户信息失败");
        }
    }

    @RequestMapping("logout")
    public Result logout(@RequestBody String userName) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.logout.authUserDTO:{}", JSON.toJSONString(userName));
            }
            Preconditions.checkNotNull(userName, "用户名不能为空");
            StpUtil.logout(userName);
            return Result.ok();
        } catch (Exception e) {
            log.error("UserController.logout.error:{}", e.getMessage());
            return Result.fail("用户登出失败");
        }
    }

}

